package com.example.finger.socket;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;

import com.example.finger.dialog.DownLoadDialog;
import com.example.finger.dialog.FingerDialog;
import com.example.finger.FingerHelper;
import com.example.finger.bean.CollectionFinger;
import com.example.finger.bean.SoketOrder;
import com.google.gson.Gson;
import com.lyxmaster.lib.toasty.Toasty;

/**
 * Created by LYX on 2018/11/14.
 */

public class ServiceBroadcastReceiver extends BroadcastReceiver {
    public static String RECVCOLLECTIONFINGERORDER = "RECVCOLLECTIONFINGERORDER";

    FingerDialog fingerDialog;
    DownLoadDialog downLoadDialog;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if(SoketOrder.CLOSE.equalsIgnoreCase(action)){
            if(fingerDialog != null && fingerDialog.isShow()){
                try{
                    fingerDialog.dismiss();
                }catch(Exception e){}

                fingerDialog = null;
            }
            if(downLoadDialog != null && downLoadDialog.isShow()){
                try{
                    downLoadDialog.dismiss();
                }catch(Exception e){}

                downLoadDialog = null;
            }
            return;
        }


        if (SoketOrder.CollectionFinger.equalsIgnoreCase(action)){
            String data = intent.getStringExtra(RECVCOLLECTIONFINGERORDER);
            CollectionFinger collectionFinger = new Gson().fromJson(data,CollectionFinger.class );

            fingerDialog = FingerDialog.getInstance(collectionFinger.getFinger() + "采集");
            fingerDialog.show(((FragmentActivity)(context)).getSupportFragmentManager(), "finger");
        }

        if (SoketOrder.PUTMANAGERFINGER.equalsIgnoreCase(action)){
            String data = intent.getStringExtra(RECVCOLLECTIONFINGERORDER);
            downLoadDialog = DownLoadDialog.getInstance(data);
            downLoadDialog.show(((FragmentActivity)(context)).getSupportFragmentManager(), "Donwload");
        }

        if (SoketOrder.DOWNLOADTASK.equalsIgnoreCase(action)){
            if(downLoadDialog != null){
                downLoadDialog.downLoad();
            }
        }
    }
}